<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        <table >
            <tr hidden>
                <td>id</td>
                <td><input type="text" name="aid"></td>
            </tr>
            <tr>
                <td>标题</td>
                <td><input type="text" name="atitle"></td>
            </tr>
            <tr>
                <td>内容</td>
                <td><input type="text" name="acontent"></td>
            </tr>
            <tr>
                <td>分类</td>
                <td><input type="text" name="aclass" id=""></td>
            </tr>
            <tr>
                <td>作者</td>
                <td><input type="text" name="aauthor"></td>
            </tr>
            <tr>
                <td><input type="button" value="保存" onclick="save()">
                <input type="button" value="取消"onclick="cancel"></td>
            </tr>
        </table>
    </div>
</body>
<script src="./js/jquery-3.6.0.min.js"></script>
<script src="./config/baseurl.js"></script>
<script src="./js/blog_api.js"></script>
<script src="./js/init.js"></script>
<script>
    $(
        function(){
            let [rUrl,params]=window.location.search.split('?')
            let a=new URLSearchParams(params)
            let id=a.get('id')
            if(id){
                getList_id(id).then(data=>{
                    $('[name=aid]').val(data.id)
                    $('[name=atitle]').val(data.title)
                    $('[name=acontent]').val(data.content)
                    $('[name=aclass]').val(data.class)
                    $('[name=aauthor]').val(data.author)

                })
            }
        }
    )

    function cancel(){
        location.href='index.html'
    }

    function save(){
        let obj={
                id:$('[name=aid]').val(),
                 title:$('name=atitle').val(),
                content:$('name=acontent').val(),
                class:$('name=aclass').val(),
                author:$('name=aauthor').val()
        }
        if(obj.id){
            putList(obj.id,obj).then(()=>{
                location.href='index.html'
            })
        }else{
            postList(obj).then(()=>{
                location.href='index.html'
            })
        }
    }
</script>
</html>